﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TarreOA.TarreOAModels;
using TarreOA.TarreOABLL;

public partial class CalendarQueryManager_AppendCalendar : System.Web.UI.Page {
    private DateTime now;
    private string userLoginName;

    protected void Page_Load(object sender, EventArgs e) {
        if(!Page.IsPostBack) {
            if(Request.QueryString.Get("u_ln") != null && Request.QueryString.Get("c_dt") != null) {
                userLoginName = Request.QueryString.Get("u_ln");
                now = DateTime.Parse(Request.QueryString.Get("c_dt")).Date;
                ViewState["userLoginName"] = userLoginName;
                ViewState["now"] = now;
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e) {
        if(ViewState["userLoginName"] != null && ViewState["now"] != null) {
            userLoginName = (string) ViewState["userLoginName"];
            now = (DateTime) ViewState["now"];
        }
        string calendarTitle = Request.Form.Get(this.TxtTitle.ClientID);
        string calendarContent = Request.Form.Get(this.TxtContent.ClientID);
        if("".Equals(calendarTitle.Trim()) || "".Equals(calendarContent.Trim())) {
            ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", "alert('信息填写不完整！');", true);
        } else {
            string startTime = ddlStartTime.Text;
            string endTime = ddlEndTime.Text;
            int intervalMinute = int.Parse(IntervalMinute.Text);
            string type = Request.Form.GetValues(this.selectCalType.ClientID)[0];
            UserCalendarArrangetbService ucatbService = new UserCalendarArrangetbService();
            int[] rev = ucatbService.saveUserCalendarArrangeInfo(calendarTitle, calendarContent, startTime, endTime, intervalMinute, now,
                type, userLoginName);
            if(rev[0] == 1) {
                ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", "alert('事务添加成功！');", true);
                CalendarArrangeInfos cainfos = CalendarArrangeInfos.instance();
                List<UserCalendarArrangetb> list = (List<UserCalendarArrangetb>) cainfos.Cainfos[userLoginName];
                if(list != null) {
                    if(now == DateTime.Parse(DateTime.Now.ToShortDateString())) {
                        list.Add(ucatbService.findByCaId(rev[1]));
                    }
                }
            } else {
                ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", "alert('事务添加失败！');", true);
            }
        }
    }
}
